﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using Traceability.API.Applications.Command.SlaughterCommand;
using Traceability.Domain;
using Traceability.ErrorCode;
using Traceability.Infrastructure;

namespace Traceability.API.Applications.CommandHandler.SlaughterCommandHandler
{
    public class SlaughterQueryCommandHandler : IRequestHandler<SlaughterQueryCommand, APIResult<Slaughter_Information>>
    {
        private readonly IBaseRepository<Slaughter_Information> _slaughterRepository;

        public SlaughterQueryCommandHandler(IBaseRepository<Slaughter_Information> slaughterRepository)
        {
            _slaughterRepository = slaughterRepository;
        }

        public Task<APIResult<Slaughter_Information>> Handle(SlaughterQueryCommand request, CancellationToken cancellationToken)
        {
            var res = _slaughterRepository.GetAll().AsNoTracking().FirstOrDefault(s => !s.IsDeltede && s.Batch == request.Batch);
            APIResult<Slaughter_Information> result = new APIResult<Slaughter_Information>();

            if(res!=null)
            {
                result.Code=EnumCode.Ok;
                result.Msg = "显示成功";
                result.Data=res;
            }
            else
            {
                result.Code = EnumCode.File;
                result.Msg = "显示失败";
            }
            return Task.FromResult(result);
        }
    }
}
